using System;
using System.Collections.Generic;

using Elev.UI.Controls;

namespace Elev.UI
{
    /// <summary>
    /// The singleton keeps a list of all available Tab Period. It can then, when needed, relaod all the grids, 
    /// after an edition of Exam and/or exam type for instance.
    /// </summary>
    internal class GridReloadNeededObserver
    {
        static GridReloadNeededObserver instance;
        static readonly object padlock = new object();

        List<IGridManager> grids;

        protected GridReloadNeededObserver()
        {
            grids = new List<IGridManager>();
        }

        static public GridReloadNeededObserver Current
        {
            get
            {
                lock (padlock)
                {
                    if (instance == null)
                        instance = new GridReloadNeededObserver();

                    return instance;
                }
            }
        }

        public void Attach(IGridManager Grid)
        {
            grids.Add(Grid);
        }

        public void Detach(IGridManager Grid)
        {
            grids.Remove(Grid);
        }

        public void DetatchAll()
        {
            grids.Clear();
        }

        public void Notify()
        {
            foreach (IGridManager grid in grids)
            {
                grid.ReloadGrid();
            }
        }
    }
}
